
#ifndef __simpe_queue_type__
#define __simpe_queue_type__

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <vector>
#include <queue>
#include <map>

//extern struct PtParam_t;
//extern void * pt_func(void * param);

template< typename _DataType , typename _ParamType>
class simple_vector_type
{

public:
	void * GetPtParam(void)
	{
		return m_pPtParam;
	}

	void  SetPtParam(void * param)
	{
		 m_pPtParam = param;
	}


	pthread_mutex_t * getmutex(void)
    {
    	return m_pMutex;
    }

	pthread_cond_t * getcondx(void)
    {
    	return m_pCond;
    }


	void setmutex( pthread_mutex_t * mutex)
    {
    	 m_pMutex = mutex;
    }

	void setcondx(pthread_cond_t * cond)
    {
    	 m_pCond = cond;
    }

protected:

	pthread_mutex_t * m_pMutex;
	pthread_cond_t * m_pCond;

	void * m_pPtParam;

private:

	std::vector<_DataType *> m_vector;
	//std::vector<_DataType *>::iterator  m_iter, m_eter;
	typedef std::vector<_DataType *>::iterator m_iterator;

public:

	simple_vector_type(void)
	{
		m_pCond = NULL;
		m_pMutex = NULL;
		m_pPtParam = NULL;
	}

	simple_vector_type( pthread_cond_t * cond , pthread_mutex_t * mutex)
	{
			m_pCond = cond;
			m_pMutex = mutex;
			m_pPtParam = NULL;
	}

	virtual ~simple_vector_type(void)
	{

	}

public:

	void lock(void)
	{
		if(m_pMutex)
		{
			pthread_mutex_lock(m_pMutex);
		}
	}

	void unlock(void)
	{
		if(m_pMutex)
		{
			pthread_mutex_unlock(m_pMutex);
		}
	}

	int size(void)
	{
		int nRet = 0x00;
		lock();
		nRet = m_vector.size();
		unlock();

		return nRet;
	}

	bool empty(void)
	{
		bool nRet = false;
		lock();
		nRet = m_vector.empty();
		unlock();

		return nRet;
	}


	virtual int destroy_all(void)
	{
		_DataType *  pPthObj = NULL;
		int nSize = 0x00;

		m_iterator iter, eter;

		lock();

		//nSize = m_vector.size();

		iter = m_vector.begin();
		eter = m_vector.end();

		for( ; iter != eter; iter ++)
		{
			pPthObj = *iter;

			if(NULL != pPthObj)
			{
				delete pPthObj;
				pPthObj = NULL;

			}

			iter = m_vector.erase(iter);
		}

		unlock();

		return nSize;
	}

	virtual pthread_t destroy_front()
	{
		pthread_t nRet = 0x00;

		lock();

		_DataType * pPthObj = m_vector.front();

		if(NULL != pPthObj)
		{
			//nRet = pPthObj->m_self_ptid;

			//pPthObj->waitstop();
			delete pPthObj;
			pPthObj = NULL;

		}
		unlock();

		return nRet;
	}

	virtual pthread_t destroy_one(_DataType * pPthObj)
	{
		return pop_one(pPthObj, true);
	}

	virtual pthread_t pop_one(_DataType * pPthObj, bool bDestroy)
	{
		pthread_t nRet = 0x00;
		int nSize = 0x00;

		_DataType *  pDataObj = NULL;

		if(NULL == pPthObj)
		{
			return -1;
		}

		lock();

		nSize = m_vector.size();
		for( int i = 0; i< nSize; i++)
		{
			pDataObj = m_vector.front();

			if(NULL == pDataObj)
			   continue;

			//if(pPthObj->m_self_ptid == pDataObj->m_self_ptid )
			if(pPthObj == pDataObj )
			{
			   //pDataObj->show();

			   if(bDestroy)
			   {
				   //pDataObj->waitstop();
				   delete pDataObj;
				   pDataObj = NULL;
			   }


			   //nRet = pPthObj->m_self_ptid;
			}else
			{
				//m_vector.push(pDataObj);
			}


		}

		unlock();

		return nRet;
	}


	virtual void show(void)
	{

	}

//	virtual void show(void)
//	{
//		lock();
//
//		_DataType *  pDataObj = NULL;
//
//		int nSize = m_queue.size();
//		for( int i = 0; i< nSize; i++)
//		{
//			pDataObj = front2back();
//
//		   if(NULL != pDataObj)
//			{
//				//pDataObj->Show();
//			}
//
//		}
//
//		unlock();
//	}

	_DataType *  front2back(void)
	{
		_DataType * pDataObj = NULL;
		pDataObj = m_vector.front();
		m_vector.pop();
		m_vector.push(pDataObj);

	     return pDataObj;
	}

	_DataType * nUsed_one(void)
	{
		return pop_front();
	}

	int nFeed_one(_DataType * pObj)
	{
		 return push_back(pObj);
	}



private:

	_DataType * pop_front(void)
	{
		_DataType * pDataObj = NULL;
		lock();

		while( m_vector.empty() )
		{
			pthread_cond_wait( m_pCond ,m_pMutex);
		}

		pDataObj = m_vector.front();
		m_vector.pop();
		unlock();

		return pDataObj;

	}

	virtual int push_back(_DataType * pData)
	{

		int nRet = 0x00;
		lock();

		if(NULL == pData)
		{
			unlock();
			return -1;
		}

//		if(0x00 == pData->m_owner_ptid)
//		{
//			pData->init(pthread_self(),m_pMutex, m_pCond, NULL );
//
//			pData->create(NULL,  NULL); // pData->create(pt_func, ( _ParamType *)m_pPtParam);
//		}
		
		m_vector.push(pData);

		pthread_cond_signal( m_pCond );
		unlock();

		return 0;
	}


};


//////////////////////////////////////////////////////


#endif  //__simpe_queue_type__
